App.controller('signController', ['$rootScope', '$scope', '$state', 'Page', function($rootScope, $scope, $state, Page) {

    Page.ajaxPost('employeeGroup/empTree.do', null, function(resp) {
        $scope.empList = resp.data;
    });

    $scope.selectable = function(data) {
        if(data.id.indexOf("_") != -1) { //选择的部门
            return false;
        }
    };

    $scope.grid = {

        save : function() {
            Page.ajaxPost($scope.sign.signId ? 'sign/update.do' : 'sign/add.do', $scope.sign, function() {
                Page.notice("操作成功!");
                $scope.grid.query();
                $scope.grid.cancel();
            });
        },
        showLocation : function(row) {
            $scope.sign = row;
            $scope.modal.show();
        },
        column_defs: [
            {
                target : 4,
                template : function(row) {
                    return '<a href="javascript:void(0)" ed-click="showLocation">位置</a>'
                }
            },
            {
                target : 6,
                buttons : [
                    {
                        name: '编辑',
                        class: 'btn-warning btm-sm',
                        icon: 'fa-edit',
                        event: 'update'
                    },
                    {
                        name: '删除',
                        class: 'btn-danger',
                        icon: 'fa-close',
                        event: function(row) {
                            Page.confirm("确定要删除吗？", function() {
                                Page.ajaxPost('sign/delete.do', row, function() {
                                    $scope.grid.query();
                                });
                            });
                        }
                    }
                ]
            }
        ]
    };

    $scope.modal = {
        onShow : function() {
            //高德
            var mapDiv = "signPositionModalMapDiv";
            var width = $('#' + mapDiv).width();
            var height = $('#' + mapDiv).height();
            $scope.map = new AMap.Map(mapDiv, {resizeEnable: true,zoom:13});

            var markers = [];
            var infoWindow = new AMap.InfoWindow({offset:new AMap.Pixel(0,-30)});
            if($scope.sign.longitude && $scope.sign.latitude){
                var marker = new AMap.Marker({
                    position: [$scope.sign.longitude, $scope.sign.latitude],
                    title:$scope.sign.empName + '-(' + $scope.sign.signTime + '-签到)',
                    map:$scope.map
                });
                marker.on('click',function(e){
                    infoWindow.setContent(e.target.getTitle());
                    infoWindow.open($scope.map, e.target.getPosition());
                });
                markers[markers.length] = marker;
            }else{
            }
            setTimeout(function() {
                if(markers.length > 1){
                    $scope.map.setFitView();
                }else if(markers.length == 1){
                    $scope.map.setCenter(markers[0].getPosition());
                }
            },300);

        }
    }

}]);